package tools
{
	import blocks.Checkpoint;
	
	import entities.Player;
	
	import org.flixel.FlxSave;
	
	public class SaveManager
	{
		
		
		public static var savedGame:Object;
		
		private static var save:FlxSave;
		private static var load:FlxSave;
		
		
		public function SaveManager() { savedGame = getSaves(); }
		
		
		public static function getSaves(saveName:String = null):Object{
			
			save = new FlxSave();
			var datas:Object;
			
			var _loaded:Boolean = save.bind("myLevelData");
			
			if(_loaded && save.data ) {
				if(save.data[Registry.gameName]){
					if(saveName && save.data[Registry.gameName][saveName]) 
						datas = save.data[Registry.gameName][saveName];
					else 
						datas = save.data[Registry.gameName];
				} else {
					datas = save.data[Registry.gameName] = [];
				}
			}
			return datas;
		}
		
		
		/**
		 * this function will save the game (checkpoint, start position, lives, health, level, abilities and score
		 * 
		 * @param	label	give a name for the save
		 * @return	nothing
		 * 
		 * */
		public static function saveGame(saveName:String):void{
			if(savedGame) {
						
				var p:Player = Registry.player;
				
				var position:Array;
				
				if(p.checkpoint) 
					position = [p.checkpoint.x, p.checkpoint.y - p.height];
				else if(Registry.gameInfos.world.startPos) 
					position = [p.startX, p.startY];
				else 
					position = [p.x, p.y];
				
				
				savedGame[saveName] =  { 
					'lives' : p.infos.gamePlay.lives,
					'health' : p.health,
					'checkpoint' : p.checkpoint ? Registry.level.checkpoints.indexOf(p.checkpoint) : null,
					'gameName' : Registry.gameName,
					'score' : p.score,
					'level' : p.level
				}
				trace('partie sauvée', saveName);
				return;
			}
			
			trace('impossible de sauvegarder');
		}
		
		
		/**
		 * this function will load the game (checkpoint, start position, lives, health, level, abilities and score
		 * 
		 * @param	label	give a name for the save, default
		 * @return	nothing
		 * 
		 * */
		
		
		public static function loadGame(saveName:String):void{
			var p:Player = Registry.player;
			
			if(savedGame){
				
				var data:Object = savedGame[saveName];
				
				
				var checkpoint:Checkpoint = data.checkpoint === null ? null : Registry.level.checkpoints[data.checkpoint];
				if (checkpoint)
					checkpoint.activate();
				p.checkpoint = checkpoint;
				
				
				p.infos.gamePlay.lives = data.lives;
				p.health = data.health;
				p.x = (checkpoint ? checkpoint.x : p.startX);
				p.y = (checkpoint ? checkpoint.y - p.height : p.startY);
				p.level = data.level;
				p.abilities.setAllowedAbilities();
				p.score = data.score;
				
			} else {
				trace('aucune partie sauvegardée pour le jeu', saveName, save.data[saveName]);
				return;
			}
			
			trace('partie chargée', saveName);
			if(Registry.pauseScreen && Registry.paused == true) Registry.pauseScreen.hidePaused();
		}
		
		
		public static function clearAllGames(label:String=null):void{
			if(savedGame)
				savedGame = null
		}
		
	}
}